package ui;

import javax.swing.*;
import java.awt.*;

public class MainJFrame extends JFrame {
    //输入框
    JTextField name = new JTextField();
    //单选框
    JRadioButton CAT = new JRadioButton();
    JRadioButton CAT_enterprise = new JRadioButton();
    //下拉框
    JComboBox<String> grade = new JComboBox<>();
    //单选框
    JRadioButton hobby_zh = new JRadioButton();
    JRadioButton hobby_en = new JRadioButton();
    //文本显示框
    JTextArea textArea_introduction = new JTextArea();
    //按钮
    JButton inZH = new JButton("中文");
    JButton inEN = new JButton("英文");

    public MainJFrame() {
        //初始化界面
        initJFrame();

        //初始化界面元素
        initView();

        //添加事件监听
        addListener();
    }

    private void addListener() {
        //事件监听
        //若姓名未填写，或专业无选择，或爱好无选择，则在文本显示框中显示提示信息
        //爱好可以多选，但专业只能选一个
        inZH.addActionListener(e -> {
            String nameStr = name.getText();
            String majorStr = "";
            if (CAT.isSelected()) {
                majorStr = CAT.getText();
            } else if (CAT_enterprise.isSelected()) {
                majorStr = CAT_enterprise.getText();
            }
            String gradeStr = (String) grade.getSelectedItem();
            String hobbyStr = "";
            if (hobby_zh.isSelected()) {
                hobbyStr += hobby_zh.getText();
            }
            if (hobby_en.isSelected()) {
                if (hobbyStr.isEmpty()) {
                    hobbyStr += hobby_en.getText();
                } else {
                    hobbyStr += "和" + hobby_en.getText();
                }
            }
            if (nameStr.isEmpty() || majorStr.isEmpty() || hobbyStr.isEmpty()) {
                textArea_introduction.setText("请填写完整信息");
            } else {
                textArea_introduction.setText("我叫" + nameStr + "，是" + gradeStr + majorStr + "专业的学生，爱好是" + hobbyStr + "。");
            }
        });

        inEN.addActionListener(e -> {
            String nameStr = name.getText();
            String majorStr = "";
            if (CAT.isSelected()) {
                majorStr = "Computer Application Technology";
            } else if (CAT_enterprise.isSelected()) {
                majorStr = "Computer Application Technology and Enterprise";
            }
            String gradeStr = (String) grade.getSelectedItem();
            String hobbyStr = "";
            if (hobby_zh.isSelected()) {
                hobbyStr += "Chinese";
            }
            if (hobby_en.isSelected()) {
                if (hobbyStr.isEmpty()) {
                    hobbyStr += "English";
                } else
                    hobbyStr += "and English";
            }
            if (nameStr.isEmpty() || majorStr.isEmpty() || hobbyStr.isEmpty()) {
                textArea_introduction.setText("Please fill in the complete information");
            } else {
                textArea_introduction.setText("My name is " + nameStr + ", a student majoring in " + majorStr + " of " + gradeStr + ", and my hobby is " + hobbyStr + ".");
            }
        });
    }

    private void initView() {

        //添加姓名输入框
        JLabel text_nameLabel = new JLabel("姓名:");

        //添加单选框
        JLabel text_major = new JLabel("专业:");
        CAT.setText("计应");
        CAT_enterprise.setText("计应企");
        ButtonGroup group_hobby = new ButtonGroup();
        group_hobby.add(CAT);
        group_hobby.add(CAT_enterprise);

        //添加下拉框
        JLabel text_grade = new JLabel("年级:");
        grade.addItem("2021");
        grade.addItem("2022");
        grade.addItem("2023");
        grade.addItem("2024");

        //添加多选框
        JLabel text_hobby = new JLabel("爱好:");
        hobby_zh.setText("中文");
        hobby_en.setText("英文");

        //添加文本显示框
        JLabel text_introduction = new JLabel("请选择中文或英文自我介绍:");
        textArea_introduction.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
        textArea_introduction.setEditable(false);
        textArea_introduction.setLineWrap(true);
        textArea_introduction.setWrapStyleWord(true);

        //设置位置
        text_nameLabel.setBounds(100, 100, 50, 20);
        name.setBounds(145, 94, 200, 30);
        text_major.setBounds(400, 100, 50, 20);
        CAT.setBounds(450, 100, 60, 20);
        CAT_enterprise.setBounds(510, 100, 100, 20);
        text_grade.setBounds(100, 150, 50, 20);
        grade.setBounds(155, 144, 100, 30);
        text_hobby.setBounds(400, 150, 50, 20);
        hobby_zh.setBounds(450, 150, 60, 20);
        hobby_en.setBounds(510, 150, 60, 20);
        text_introduction.setBounds(100, 250, 400, 20);
        textArea_introduction.setBounds(350, 200, 300, 200);
        inZH.setBounds(250, 450, 100, 30);
        inEN.setBounds(400, 450, 100, 30);

        //设置字体
        Font font = new Font("Dialog", Font.BOLD, 16);
        text_nameLabel.setFont(font);
        text_major.setFont(font);
        text_grade.setFont(font);
        text_hobby.setFont(font);
        text_introduction.setFont(font);
        name.setFont(font);
        CAT.setFont(font);
        CAT_enterprise.setFont(font);
        grade.setFont(font);
        hobby_zh.setFont(font);
        hobby_en.setFont(font);

        //添加到界面中
        this.getContentPane().add(text_nameLabel);
        this.getContentPane().add(name);
        this.getContentPane().add(text_major);
        this.getContentPane().add(CAT);
        this.getContentPane().add(CAT_enterprise);
        this.getContentPane().add(text_grade);
        this.getContentPane().add(grade);
        this.getContentPane().add(text_hobby);
        this.getContentPane().add(hobby_zh);
        this.getContentPane().add(hobby_en);
        this.getContentPane().add(text_introduction);
        this.getContentPane().add(textArea_introduction);
        this.getContentPane().add(textArea_introduction);
        this.getContentPane().add(inZH);
        this.getContentPane().add(inEN);
    }

    private void initJFrame() {
        this.setTitle("郑凯飞 (2023126144)");//设置标题
        this.setSize(800, 600);//设置窗体大小
        this.setAlwaysOnTop(true);//设置界面置顶
        this.setLocationRelativeTo(null);//设置窗体居中
        this.setLayout(null);//取消默认的居中放置
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置关闭窗体的时候，退出程序
        this.setVisible(true);//设置窗体可见
    }
}
